<template>
  <div>
    <el-dialog
      :title="isEdit ?'编辑资源':'添加资源'"
      :visible="dialogVisible"
      @close="$emit('watchChild')"
      @open="openDialog">
      <el-form :model="resform" class="dialog_form">
        <el-form-item :label="'资源名称'" :label-width="formLabelWidth">
          <el-input v-model="resform.name"></el-input>
        </el-form-item>
        <el-form-item :label="'资源路径'" :label-width="formLabelWidth">
          <el-input v-model="resform.url"></el-input>
        </el-form-item>
        <el-form-item label="资源分类" :label-width="formLabelWidth">
          <el-select v-model="resform.categoryId" placeholder="请选择分类">
             <!-- <el-option :value="-1" label="角色管理"></el-option> -->
            <el-option
              v-for="item in resData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
           <el-input type="textarea" v-model="resform.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('watchChild')">取 消</el-button>
        <el-button type="primary" @click="onConfirms">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { getAllCategory, saveOrUpdate } from '@/services/resource'
import Vue from 'vue'
export default Vue.extend({
  name: 'AddResource',
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    currentData: {
      type: Object
    }
  },
  data () {
    return {
      // dialogFormVisible: false,
      resform: {
        name: '',
        categoryId: -1,
        url: '',
        description: ''
      },
      formLabelWidth: '70px',
      resData: []
    }
  },
  methods: {
    // recordResourceBol (val:boolean) {
    //   // console.log(this.dialogVisible)
    //   this.dialogFormVisible = val// this.dialogVisible
    // },
    async onConfirms () {
      this.$confirm('此操作将添加一项资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log('添加信息', this.resform)

        const { data } = await saveOrUpdate(this.resform)
        // console.log('添加资源', data)
        if (data.code === '000000') {
          this.$message.success('添加成功')
          this.handlerAllCate()
          // this.dialogFormVisible = false
          this.$emit('watchChild')
        }
      }).catch(() => {
        this.$message.error('添加失败，请重试')
      })
    },
    async handlerAllCate () {
      const { data } = await getAllCategory()
      // console.log('资源分类', data)
      if (data.code === '000000') {
        this.resData = data.data
      }
    },
    openDialog () {
      if (this.isEdit && this.currentData) {
        this.resform = this.currentData
      } else {
        this.resform.name = ''
        this.resform.categoryId = -1
        this.resform.url = ''
        this.resform.description = ''
      }
    }
  },
  created () {
    // 分类列表
    this.handlerAllCate()
  }
})
</script>

<style lang="scss" scoped>

.dialog_form{
  width: 70%;
  margin-left: 2rem;
}
</style>
